package com.akk.module.yb.service;

import com.akk.PayConstants;
import com.akk.module.hlbpay.util.HlbBeanUtils;
import com.akk.module.yb.model.*;
import com.akk.module.yb.utils.AESUtil;
import com.akk.utils.IDUtils;
import com.akk.utils.RestResponse;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.LaxRedirectStrategy;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class YbService {

    @Value("${yb.photo}")
    private String photoPath;

    public RestResponse register(RegisterRequest registerRequest) {
        registerRequest.setMainCustomerNumber(PayConstants.YB_AGENT_NUMBER);
        String hmac = RegisterRequest.signature(registerRequest);
        registerRequest.setHmac(hmac);
        registerRequest.setLoginPassword(AESUtil.encrypt(registerRequest.getLoginPassword(), PayConstants.YB_PARAMS_HMAC_KEY));
        registerRequest.setTradePassword(AESUtil.encrypt(registerRequest.getTradePassword(), PayConstants.YB_PARAMS_HMAC_KEY));
        System.out.println(new Gson().toJson(registerRequest));
        Part[] parts = new Part[0];
        try {
            parts = RegisterRequest.builderPart(registerRequest);
        } catch (FileNotFoundException e) {
        }
        PostMethod postMethod = new PostMethod(PayConstants.YB_REGISTER_URL);
        HttpClient client = new HttpClient();
        postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));
        RestResponse restResponse;
        try {
            int status = client.executeMethod(postMethod);
            if (status == HttpStatus.SC_OK) {
//                System.out.println(postMethod.getResponseBodyAsString());
                JsonObject object = new JsonParser().parse(postMethod.getResponseBodyAsString()).getAsJsonObject();
                System.out.println(new Gson().toJson(object));
                if (object.has("code") && object.get("code").getAsString().equals("0000")) {
                    return RestResponse.build(object.get("customerNumber").getAsString());
                }
                restResponse = RestResponse.build(RestResponse.AVS_ERROR_CODE, object.get("message").getAsString());
            } else if (status == HttpStatus.SC_MOVED_PERMANENTLY || status == HttpStatus.SC_MOVED_TEMPORARILY) {
                // 从头中取出转向的地址
                Header locationHeader = postMethod.getResponseHeader("location");
                String location = null;
                if (locationHeader != null) {
                    location = locationHeader.getValue();
                    restResponse = RestResponse.build(RestResponse.AVS_ERROR_CODE, "系统繁忙");
                } else {
                    restResponse = RestResponse.build(RestResponse.AVS_ERROR_CODE, "系统繁忙");
                }
            } else {
                restResponse = RestResponse.build(RestResponse.AVS_ERROR_CODE, "系统繁忙:" + status);
            }
            return restResponse;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 释放连接
            postMethod.releaseConnection();
        }
        restResponse = RestResponse.build(RestResponse.AVS_ERROR_CODE, "系统繁忙");
        return restResponse;
    }

    // 查询商户信息
    public RestResponse queryInfo(YbQueryInfoRequest request) {
        request.setMainCustomerNumber(PayConstants.YB_AGENT_NUMBER);
//        request.setCustomerNumber(null);
        HttpClient client = new HttpClient();
        PostMethod postMethod = new PostMethod(PayConstants.YB_QUERY_INFO_URL);
        try {
            String hmac = YbQueryInfoRequest.signature(request);
            request.setHmac(hmac);
            System.out.println(new Gson().toJson(request));
            Part[] parts = YbQueryInfoRequest.builderPart(request);
            postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));
            int status = client.executeMethod(postMethod);
            if (status == HttpStatus.SC_OK) {
                String result = postMethod.getResponseBodyAsString();
                YbQueryInfoResponse response = new Gson().fromJson(result, YbQueryInfoResponse.class);
                if (response.getCode().equals("0000")) {
                    return RestResponse.build(response);
                } else {
                    return RestResponse.build(RestResponse.AVS_ERROR_CODE, response.getMessage(), response);
                }

            } else {
                return RestResponse.build(RestResponse.AVS_ERROR_CODE, "支付系统繁忙");
            }
        } catch (IOException e) {
            return RestResponse.build(RestResponse.AVS_ERROR_CODE, "支付系统繁忙");
        } finally {
            postMethod.releaseConnection();

        }
    }

    // 审核
    public RestResponse auditMerchant(AuditMerchantRequest request) {
        request.setMainCustomerNumber(PayConstants.YB_AGENT_NUMBER);
        HttpClient client = new HttpClient();
        PostMethod postMethod = new PostMethod(PayConstants.YB_AUDIT_MERCHANT_URL);
        try {
            String hmac = AuditMerchantRequest.signature(request);
            request.setHmac(hmac);
            Part[] parts = AuditMerchantRequest.builderPart(request);
            postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));
            int status = client.executeMethod(postMethod);
            if (status == HttpStatus.SC_OK) {
                String result = postMethod.getResponseBodyAsString();
                AuditMerchantResponse response = new Gson().fromJson(result, AuditMerchantResponse.class);
                if (response.getCode().equals("0000")) {
                    return RestResponse.build(response);
                } else {
                    return RestResponse.build(RestResponse.AVS_ERROR_CODE, response.getMessage(), response);
                }
            } else {
                return RestResponse.build(RestResponse.AVS_ERROR_CODE, "支付系统繁忙");
            }
        } catch (IOException e) {
            return RestResponse.build(RestResponse.AVS_ERROR_CODE, "支付系统繁忙");
        } finally {
            postMethod.releaseConnection();

        }
    }

    // 修改商户信息
    public RestResponse editMerchant(EditMerchantRequest request) {
        request.setMainCustomerNumber(PayConstants.YB_AGENT_NUMBER);
        HttpClient client = new HttpClient();
        PostMethod postMethod = new PostMethod(PayConstants.YB_EDIT_MERCHANT_URL);
        try {
            String hmac = EditMerchantRequest.signature(request);
            request.setHmac(hmac);
            Part[] parts = EditMerchantRequest.builderPart(request);
            postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));
            int status = client.executeMethod(postMethod);
            if (status == HttpStatus.SC_OK) {
                String result = postMethod.getResponseBodyAsString();
                EditMerchantResponse response = new Gson().fromJson(result, EditMerchantResponse.class);
                if (response.getCode().equals("0000")) {
                    return RestResponse.build(response);
                } else {
                    return RestResponse.build(RestResponse.AVS_ERROR_CODE, response.getMessage(), response);
                }
            } else {
                return RestResponse.build(RestResponse.AVS_ERROR_CODE, "支付系统繁忙");
            }
        } catch (IOException e) {
            return RestResponse.build(RestResponse.AVS_ERROR_CODE, "支付系统繁忙");
        } finally {
            postMethod.releaseConnection();

        }
    }

    // 修改商户手机号
    public RestResponse updatePhone(String merId, String phone) {
        EditMerchantRequest request = new EditMerchantRequest();
        request.setMainCustomerNumber(PayConstants.YB_AGENT_NUMBER);
        request.setCustomerNumber(merId);
        request.setModifyType("6");
        request.setBindMobile(phone);
        return editMerchant(request);
    }

//    // 查询商户结算记录
//    public RestResponse depositRecordQuery(){
//
//    }

    // 修改提现费率
    public RestResponse feeSet(FeeSetRequest request) {
        request.setMainCustomerNumber(PayConstants.YB_AGENT_NUMBER);
        HttpClient client = new HttpClient();
        PostMethod postMethod = new PostMethod(PayConstants.YB_FEE_SET_URL);
        try {
            String hmac = FeeSetRequest.signature(request);
            request.setHmac(hmac);
            Part[] parts = FeeSetRequest.builderPart(request);
            postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));
            int status = client.executeMethod(postMethod);
            if (status == HttpStatus.SC_OK) {
                String result = postMethod.getResponseBodyAsString();
                FeeSetResponse response = new Gson().fromJson(result, FeeSetResponse.class);
                if (response.getCode().equals("0000")) {
                    return RestResponse.build(response);
                } else {
                    return RestResponse.build(RestResponse.AVS_ERROR_CODE, response.getMessage(), response);
                }
            } else {
                return RestResponse.build(RestResponse.AVS_ERROR_CODE, "支付系统繁忙");
            }
        } catch (IOException e) {
            return RestResponse.build(RestResponse.AVS_ERROR_CODE, "支付系统繁忙");
        } finally {
            postMethod.releaseConnection();
        }
    }

    // 商户费率查询
    public RestResponse feeQuery(MerFeeQueryRequest request) {
        request.setMainCustomerNumber(PayConstants.YB_AGENT_NUMBER);
        request.setHmac(MerFeeQueryRequest.signature(request));
        HttpClient client = new HttpClient();
        PostMethod postMethod = new PostMethod(PayConstants.YB_QUERY_FEE_SET_API_URL);
        try {
            Part[] parts = MerFeeQueryRequest.builderPart(request);
            postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));
            int status = client.executeMethod(postMethod);
            if (status == HttpStatus.SC_OK) {
                String result = postMethod.getResponseBodyAsString();
                MerFeeQueryResponse response = new Gson().fromJson(result, MerFeeQueryResponse.class);
                if (response.getCode().equals("0000")) {
                    return RestResponse.build(response.getRate());
                } else {
                    return RestResponse.build(RestResponse.AVS_ERROR_CODE, response.getMessage(), response);
                }
            } else {
                return RestResponse.build(RestResponse.AVS_ERROR_CODE, "支付系统繁忙");
            }
        } catch (IOException e) {
            return RestResponse.build(RestResponse.AVS_ERROR_CODE, "支付系统繁忙");
        } finally {
            postMethod.releaseConnection();
        }
    }

    // 结算记录查询
    public RestResponse depositRecordQuert(DepositRecordQueryRequest request) {
        request.setMainCustomerNumber(PayConstants.YB_AGENT_NUMBER);
        HttpClient client = new HttpClient();
        PostMethod postMethod = new PostMethod(PayConstants.YB_WITHDRAW_QUERY_API_URL);
        try {
            String hmac = DepositRecordQueryRequest.signature(request);
            request.setHmac(hmac);
            System.out.println(new Gson().toJson(request));
            Part[] parts = DepositRecordQueryRequest.builderPart(request);
            postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));
            int status = client.executeMethod(postMethod);
            if (status == HttpStatus.SC_OK) {
                String result = postMethod.getResponseBodyAsString();
                DepositRecordQueryResponse response = new Gson().fromJson(result, DepositRecordQueryResponse.class);
                if (response.getCode().equals("0000")) {
                    return RestResponse.build(response);
                } else {
                    return RestResponse.build(RestResponse.AVS_ERROR_CODE, response.getMessage(), response);
                }
            } else {
                return RestResponse.build(RestResponse.AVS_ERROR_CODE, "支付系统繁忙");
            }
        } catch (IOException e) {
            return RestResponse.build(RestResponse.AVS_ERROR_CODE, "支付系统繁忙");
        } finally {
            postMethod.releaseConnection();
        }
    }

    // 收款
    public RestResponse receive(ReceiveRequest request) {
        request.setMainCustomerNumber(PayConstants.YB_AGENT_NUMBER);
        HttpClient client = new HttpClient();
        PostMethod postMethod = new PostMethod(PayConstants.YB_RECEIVE_API_URL);
        try {
            String hmac = ReceiveRequest.signature(request);
            request.setHmac(hmac);
            System.out.println(new Gson().toJson(request));
            Part[] parts = ReceiveRequest.builderPart(request);
            postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));
            int status = client.executeMethod(postMethod);
            if (status == HttpStatus.SC_OK) {
                String result = postMethod.getResponseBodyAsString();
                ReceiveResponse response = new Gson().fromJson(result, ReceiveResponse.class);
                if (response.getCode().equals("0000")) {
                    response.setUrl(AESUtil.decrypt(response.getUrl(), PayConstants.YB_PARAMS_HMAC_KEY.substring(0, 16)));
                    return RestResponse.build(response);
                } else {
                    return RestResponse.build(RestResponse.AVS_ERROR_CODE, response.getMessage(), response);
                }
            } else {
                return RestResponse.build(RestResponse.AVS_ERROR_CODE, "支付系统繁忙");
            }
        } catch (IOException e) {
            return RestResponse.build(RestResponse.AVS_ERROR_CODE, "支付系统繁忙");
        } finally {
            postMethod.releaseConnection();
        }

    }


    // 交易查询
    public RestResponse receiveQuery(ReceiveQueryRequest request) {
        request.setMainCustomerNumber(PayConstants.YB_AGENT_NUMBER);
        HttpClient client = new HttpClient();
        PostMethod postMethod = new PostMethod(PayConstants.YB_RECEIVE_QUERY_API_URL);
        try {
            String hmac = ReceiveQueryRequest.signature(request);
            request.setHmac(hmac);
            Part[] parts = ReceiveQueryRequest.builderPart(request);
            postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));
            int status = client.executeMethod(postMethod);
            if (status == HttpStatus.SC_OK) {
                String result = postMethod.getResponseBodyAsString();
                ReceiveQueryResponse response = new Gson().fromJson(result, ReceiveQueryResponse.class);
                if (response.getCode().equals("0000")) {
                    if (response.getTradeReceives().isEmpty()) {
                        return RestResponse.build(RestResponse.AVS_ERROR_CODE, "未查询到记录");
                    } else {
                        return RestResponse.build(response);
                    }
                } else {
                    return RestResponse.build(RestResponse.AVS_ERROR_CODE, response.getMessage(), response);
                }
            } else {
                return RestResponse.build(RestResponse.AVS_ERROR_CODE, "支付系统繁忙");
            }
        } catch (IOException e) {
            return RestResponse.build(RestResponse.AVS_ERROR_CODE, "支付系统繁忙");
        } finally {
            postMethod.releaseConnection();
        }
    }


    // 结算提现
    public RestResponse withdraw(WithdrawRequest request) {
        request.setMainCustomerNumber(PayConstants.YB_AGENT_NUMBER);
        request.setHmac(WithdrawRequest.signature(request));
        HttpClientBuilder builder = HttpClients.custom()
                .disableAutomaticRetries() // 关闭自动处理重定向
                .setRedirectStrategy(new LaxRedirectStrategy());//利用LaxRedirectStrategy处理POST重定向问题
        CloseableHttpClient closeableHttpClient = builder.build();
        HttpPost httpPost = new HttpPost(PayConstants.YB_WITHDRAW_API_URL);
        try {
            Map<String, String> params = HlbBeanUtils.convertBean(request, new LinkedHashMap());
            System.out.println("提现req:" + new Gson().toJson(params));
            List<NameValuePair> pairList = new ArrayList<>();
            for (String key : params.keySet()) {
                pairList.add(new BasicNameValuePair(key, params.get(key)));
            }
            httpPost.setEntity(new UrlEncodedFormEntity(pairList, "UTF-8"));
            CloseableHttpResponse httpResponse = closeableHttpClient.execute(httpPost);
            if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                HttpEntity entity = httpResponse.getEntity();
                String result = EntityUtils.toString(entity);
                WithdrawResponse response = new Gson().fromJson(result, WithdrawResponse.class);
                System.out.println("提现res:" + result);
                if (response.getCode().equals("0000")) {
                    return RestResponse.build(response);
                } else {
                    return RestResponse.build(RestResponse.AVS_ERROR_CODE, response.getMessage(), response);
                }
            } else {
                return RestResponse.build(RestResponse.AVS_ERROR_CODE, "支付系统繁忙");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RestResponse.build(RestResponse.AVS_ERROR_CODE, "支付系统繁忙");
    }


    // 结算查询
    public RestResponse withdrawQuery(WithdrawQueryRequest request) {
        request.setMainCustomerNumber(PayConstants.YB_AGENT_NUMBER);
        HttpClient client = new HttpClient();
        PostMethod postMethod = new PostMethod(PayConstants.YB_WITHDRAW_QUERY_API_URL);
        try {
            String hmac = WithdrawQueryRequest.signature(request);
            request.setHmac(hmac);
            System.out.println("req:" + new Gson().toJson(request));
            Part[] parts = WithdrawQueryRequest.builderPart(request);
            postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));
            int status = client.executeMethod(postMethod);
            System.out.println("res:" + postMethod.getResponseBodyAsString());
            if (status == HttpStatus.SC_OK) {
                String result = postMethod.getResponseBodyAsString();
                WithdrawQueryResponse response = new Gson().fromJson(result, WithdrawQueryResponse.class);
                if (response.getCode().equals("0000")) {
                    if (response.getTransferRequests().isEmpty()) {
                        return RestResponse.build("1001", "没有记录");
                    } else
                        return RestResponse.build(response);
                } else {
                    return RestResponse.build(RestResponse.AVS_ERROR_CODE, response.getMessage(), response);
                }
            } else {
                return RestResponse.build(RestResponse.AVS_ERROR_CODE, "支付系统繁忙");
            }
        } catch (IOException e) {
            return RestResponse.build(RestResponse.AVS_ERROR_CODE, "支付系统繁忙");
        } finally {
            postMethod.releaseConnection();
        }
    }

    // 可用余额查询
    public RestResponse balanceQuery(String ybSubMerId, String type) {
        QueryCustomerBalanceRequest request = new QueryCustomerBalanceRequest();
        request.setBalanceType(type);
        request.setCustomerNumber(ybSubMerId);
        request.setMainCustomerNumber(PayConstants.YB_AGENT_NUMBER);
        String hmac = QueryCustomerBalanceRequest.signature(request);
        HttpClient client = new HttpClient();
        PostMethod postMethod = new PostMethod(PayConstants.YB_QUERY_BALANCE_API_URL);
        try {
            request.setHmac(hmac);
            System.out.println("req:" + new Gson().toJson(request));
            Part[] parts = QueryCustomerBalanceRequest.builderPart(request);
            postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));
            int status = client.executeMethod(postMethod);
            System.out.println("res:" + postMethod.getResponseBodyAsString());
            if (status == HttpStatus.SC_OK) {
                String result = postMethod.getResponseBodyAsString();
                QueryCustomerBalanceResponse response = new Gson().fromJson(result, QueryCustomerBalanceResponse.class);
                if (response.getCode().equals("0000")) {
                    return RestResponse.build(response);
                } else {
                    return RestResponse.build(RestResponse.AVS_ERROR_CODE, response.getMessage(), response);
                }
            } else {
                return RestResponse.build(RestResponse.AVS_ERROR_CODE, "支付系统繁忙");
            }
        } catch (IOException e) {
            return RestResponse.build(RestResponse.AVS_ERROR_CODE, "支付系统繁忙");
        } finally {
            postMethod.releaseConnection();
        }
    }

    // 分润查询
    public RestResponse tradeInfo(String beginDate, String endDate) {
        TradeInfoQueryRequest request = new TradeInfoQueryRequest();
        request.setMainCustomerNumber(PayConstants.YB_AGENT_NUMBER);
        request.setSplitterType("TRADE_SPLITTER");
        request.setCustomerNumber(PayConstants.YB_AGENT_NUMBER);
        request.setBeginDate(beginDate);
        request.setEndDate(endDate);
        request.setPageNo("1");
        request.setHmac(TradeInfoQueryRequest.signature(request));
        HttpClient client = new HttpClient();
        PostMethod postMethod = new PostMethod(PayConstants.YB_QUERY_TRADE_INFO_API_URL);
        try {
            Part[] parts = TradeInfoQueryRequest.builderPart(request);
            postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));
            int status = client.executeMethod(postMethod);
            System.out.println("res:" + postMethod.getResponseBodyAsString());
            if (status == HttpStatus.SC_OK) {
                String result = postMethod.getResponseBodyAsString();
                TradeInfoQueryResponse response = new Gson().fromJson(result, TradeInfoQueryResponse.class);
                if (response.getCode().equals("0000")) {
                    return RestResponse.build(response);
                } else {
                    return RestResponse.build(RestResponse.AVS_ERROR_CODE, response.getMessage(), response);
                }
            } else {
                return RestResponse.build(RestResponse.AVS_ERROR_CODE, "支付系统繁忙");
            }
        } catch (IOException e) {
            return RestResponse.build(RestResponse.AVS_ERROR_CODE, "支付系统繁忙");
        } finally {
            postMethod.releaseConnection();
        }
    }

    // 分润转账
    public RestResponse transferToCustomer(String amount) {
        TransferToCustomerRequest request = new TransferToCustomerRequest();
        request.setTransAmount(amount);
        request.setCustomerNumber(PayConstants.YB_TRANS_TO_CUSTOMER);
        request.setRequestId(IDUtils.getUniqueNum14());
        request.setMainCustomerNumber(PayConstants.YB_AGENT_NUMBER);
        request.setRemark("分润转出");
        request.setHmac(TransferToCustomerRequest.signature(request));
        HttpClient client = new HttpClient();
        PostMethod postMethod = new PostMethod(PayConstants.YB_QUERY_TRANS_TO_CUSTOMER_API_URL);
        try {
            Part[] parts = TransferToCustomerRequest.builderPart(request);
            postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));
            int status = client.executeMethod(postMethod);
            System.out.println("res:" + postMethod.getResponseBodyAsString());
            if (status == HttpStatus.SC_OK) {
                String result = postMethod.getResponseBodyAsString();
                TransferToCustomerResponse response = new Gson().fromJson(result, TransferToCustomerResponse.class);
                if (response.getCode().equals("0000")) {
                    return RestResponse.build(response);
                } else {
                    return RestResponse.build(RestResponse.AVS_ERROR_CODE, response.getMessage(), response);
                }
            } else {
                return RestResponse.build(RestResponse.AVS_ERROR_CODE, "支付系统繁忙");
            }
        } catch (IOException e) {
            return RestResponse.build(RestResponse.AVS_ERROR_CODE, "支付系统繁忙");
        } finally {
            postMethod.releaseConnection();
        }
    }

    @Test
    public void testTrade() {
        RestResponse restResponse = tradeInfo("2017-11-01 00:00:00", "2017-11-30 00:00:00");
        System.out.println(new Gson().toJson(restResponse));
    }

    @Test
    public void test1() {
        RegisterRequest request = new RegisterRequest();
        request.setCustomerType("PERSON");
        request.setBankCardPhoto(photoPath);
        request.setIdCardBackPhoto(photoPath);
        request.setIdCardPhoto(photoPath);
        request.setBusinessLicensePhoto(photoPath);
        request.setPersonPhoto(photoPath);
        request.setMainCustomerNumber(PayConstants.YB_AGENT_NUMBER);
        request.setRequestId("74e9b553-2271-4");
        request.setLoginPassword("");
        request.setLoginPasswordConfirm("");
        request.setTradePassword("");
        request.setTradePasswordConfirm("");
        request.setCertFee("1");
        request.setMailStr("");
        request.setBusinessLicence("");
        request.setBankAccountType("PrivateCash");
        request.setBindMobile("17761265912");
        request.setSignedName("李正");
        request.setLinkMan("周小方");
        request.setIdCard("511921199508216738");
        request.setLegalPerson("李正");
        request.setMinSettleAmount("10");
        request.setRiskReserveDay("0");
        request.setSplitter("");
        request.setSplitterprofitfee("");
        request.setFreezeDays("19");
        request.setBankAccountNumber("6231561600104912");
        request.setBankName("光大银行");
        request.setAccountName("李正");
        request.setAreaCode("1000");
        request.setWhiteList("0");
        request.setManualSettle("Y");
        register(request);
    }

    @Test
    public void test2() {
        YbQueryInfoRequest request = new YbQueryInfoRequest();
        request.setMobilePhone("13701388229");
        request.setCustomerType("CUSTOMER");
        request.setMainCustomerNumber(PayConstants.YB_AGENT_NUMBER);
        RestResponse response = queryInfo(request);
        System.out.println(new Gson().toJson(response));
    }

    @Test
    public void test3() {
        AuditMerchantRequest request = new AuditMerchantRequest();
        request.setCustomerNumber("10021318185");
        request.setStatus("SUCCESS");
        RestResponse response = auditMerchant(request);
        System.out.println(new Gson().toJson(response));
    }

    @Test
    public void test4() {
        EditMerchantRequest request = new EditMerchantRequest();
        request.setModifyType("1");
        request.setCustomerNumber("10016279034");
        request.setWhiteList("1");
        RestResponse response = editMerchant(request);
        System.out.println(new Gson().toJson(response));
    }

    @Test
    public void test5() {
        ReceiveRequest request = new ReceiveRequest();
        request.setCustomerNumber("10016074267");
        request.setMobileNumber("17761265912");
        request.setRequestId(UUID.randomUUID().toString().substring(20));
        request.setSource("S");
        request.setAmount("1");
        request.setMcc("5311");
        request.setCallBackUrl("http://www.baidu.com");
        request.setWebCallBackUrl("http://www.baidu.com");
        request.setSmgCallBackUrl("http://www.baidu.com");
        request.setPayerBankAccountNo("6231561600104912");
        RestResponse restResponse = receive(request);
        System.out.println(restResponse);
    }

    @Test
    public void test7() {
        WithdrawRequest request = new WithdrawRequest();
        request.setAmount("1");
        request.setCallBackUrl("http://www.baidu.com");
        request.setCustomerNumber("10016074267");
        request.setExternalNo(UUID.randomUUID().toString());
        request.setTransferWay("1");
        request.setHmac(WithdrawRequest.signature(request));
        RestResponse restResponse = withdraw(request);
        System.out.println(restResponse);
    }

    @Test
    public void test6() {
        ReceiveQueryRequest request = new ReceiveQueryRequest();
        request.setCustomerNumber("10018135037");
//        request.setRequestId("CT82990070822015");
        request.setCreateTimeBegin("2018-03-10 00:00:00");
        request.setCreateTimeEnd("2018-04-07 23:00:00");
        request.setPageNo("1");
        RestResponse response = receiveQuery(request);
        System.out.println(new Gson().toJson(response));
    }

    @Test
    public void test8() {
        WithdrawQueryRequest request = new WithdrawQueryRequest();
        request.setMainCustomerNumber(PayConstants.YB_AGENT_NUMBER);
        request.setCustomerNumber("10018135037");
        request.setExternalNo("18040917185942351");
        request.setRequestDateSectionBegin("2018-04-07 00:00:00");
        request.setRequestDateSectionEnd("2018-04-22 23:00:00");
        request.setTransferWay("1");
        request.setPageNo("1");
        request.setHmac(WithdrawQueryRequest.signature(request));
        System.out.println(new Gson().toJson(request));
        RestResponse restResponse = withdrawQuery(request);
        System.out.println(new Gson().toJson(restResponse.getResult()));
    }

    // 查询结算记录
    @Test
    public void depositRecordQueryTest() {
        DepositRecordQueryRequest recordQueryRequest = new DepositRecordQueryRequest();
        recordQueryRequest.setCustomerNumber("10018622175");
        recordQueryRequest.setRequestDateSectionBegin("2018-02-05 00:00:00");
        recordQueryRequest.setRequestDateSectionEnd("2018-02-07 00:00:00");
        recordQueryRequest.setTransferWay("1");// 1 T0 2 T1 3 T1自动
        recordQueryRequest.setPageNo("1");
        RestResponse restResponse = depositRecordQuert(recordQueryRequest);
        System.out.println(new Gson().toJson(restResponse));
    }

    @Test
    public void testFeeQuery() {
        MerFeeQueryRequest request = new MerFeeQueryRequest();
        request.setCustomerNumber("10017058674");
        request.setProductType("1");
        RestResponse restResponse = feeQuery(request);
        System.out.println(new Gson().toJson(restResponse));
    }

    @Test
    public void updatePhone() {
        RestResponse restResponse = updatePhone("10019239347", "13924237846");
        System.out.println(new Gson().toJson(restResponse));
    }

//    @Test
//    public void test22(){
//        AuditMerchantRequest request = new AuditMerchantRequest();
//        request.setStatus("");
//        auditMerchant();
//    }

}

